package com.wlts.wzy.TestDemo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wlts.wzy.DYGoodsModule.DYapi.DyApiClient;
import com.wlts.wzy.DYOrderModule.entity.DyOrders;
import com.wlts.wzy.DYUserModule.entity.DyStoreUser;
import com.wlts.wzy.DYUserModule.service.IDyStoreUserService;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.api.request.dy.GetMyDyOrdersRequest;
import com.wlts.wzy.api.response.dy.UpdateDyOrdersResponse;
import com.wlts.wzy.configBean.DyConfig.DyConfig;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.expand.MyPage;
import com.wlts.wzy.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import springfox.documentation.spring.web.json.Json;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

public class TestDYDemo {
    private static final String appKey = "6959524271349597727";
    private static String appSecret = "0ce1e03c-c5b0-4ddf-83ab-f697c5c1049e";
    private static String accessToken = "2e093181-d9d3-44b3-aa53-2590f5c9aa6f";
    private static String baseUrl = "https://openapi-fxg.jinritemai.com";

    public void getOrderList() {
        String Api = baseUrl + "/order/searchList";
        String method = "order.searchList";

        TreeMap<String, String> paramJson = new TreeMap<>();

        paramJson.put("page", "0");
        paramJson.put("size", "100");
        paramJson.put("create_time_start", "1633532157");

        JSONObject object = DyApiClient.DYAoiGet(Api, method, appKey, appSecret, accessToken, paramJson);


        JSONArray jsonArray = object.getJSONObject("data").getJSONArray("shop_order_list");

        if (jsonArray != null) {
            List<UpdateDyOrdersResponse> responseList = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            for (int i = 0; i < jsonArray.size(); i++) {

                JSONObject item = jsonArray.getJSONObject(i);
                String shopId = item.getString("shop_id");
                JSONArray logisticsInfo = item.getJSONArray("logistics_info");
                String logisticsId = "0";
                String logisticsCode = "0";
                if (logisticsInfo.size() > 0) {
                    String company = logisticsInfo.getJSONObject(0).getString("company");
                    logisticsId = getLogisticsId(company);
                    logisticsCode = logisticsInfo.getJSONObject(0).getString("tracking_no");
                }
                JSONArray child = item.getJSONArray("sku_order_list");
                JSONArray newChild = new JSONArray();
                for(int j = 0; j < child.size(); j++) {
                    JSONObject childItem = child.getJSONObject(j);
                    String orderId = childItem.getString("order_id");
                    String cipherText = childItem.getString("encrypt_post_tel");
                    String postTel = decode(cipherText, orderId);
                    childItem.put("post_tel", postTel);
                    cipherText = childItem.getString("encrypt_post_receiver");
                    String postRec = decode(cipherText, orderId);
                    childItem.put("post_receiver", postRec);
                    JSONObject post_addr = childItem.getJSONObject("post_addr");
                    cipherText = post_addr.getString("encrypt_detail");
                    String detail = decode(cipherText, orderId);
                    Long payTime = childItem.getLong("pay_time");
                    if (payTime > 0) {
                        String payTimeFormat = sdf.format(payTime * 1000);
                        childItem.put("pay_time", payTimeFormat);
                    }
                    post_addr.put("detail", detail);
                    childItem.put("post_addr", post_addr);
                    childItem.put("pid", childItem.getString("parent_order_id"));
                    childItem.put("final_status", childItem.getString("order_status"));
                    childItem.put("combo_num", childItem.getString("item_num"));
                    childItem.put("combo_amount", childItem.getString("origin_amount"));
                    childItem.put("spec_desc", childItem.getJSONArray("spec").toJSONString());
                    childItem.put("logistics_time", childItem.getString("ship_time"));
                    childItem.put("total_amount", childItem.getString("pay_amount"));
                    childItem.put("logistics_time", childItem.getString("ship_time"));
                    childItem.put("receipt_time", childItem.getString("logistics_receipt_time"));
                    childItem.put("shop_id", shopId);
                    childItem.put("logistics_id", logisticsId);
                    childItem.put("urge_cnt", "0");
                    if (!logisticsCode.equals("0")) {
                        childItem.put("logistics_code", logisticsCode);
                    }
                    newChild.add(childItem);
                }
                List<UpdateDyOrdersResponse> array = JSONArray.parseArray(newChild.toJSONString(), UpdateDyOrdersResponse.class);
                System.out.println(array);
                System.out.println("====================");
//                responseList.addAll(array);
            }

        } else {
            System.out.println(object);
        }
    }

    public void getCategory() {
        String Api = baseUrl + "/shop/getShopCategory";
        String method = "shop.getShopCategory";

        TreeMap<String, String> paramJson = new TreeMap<>();

        paramJson.put("cid", "20313");
//        paramJson.put("size", "100");
//        paramJson.put("create_time_start", "1633532157");

        JSONObject object = DyApiClient.DYAoiGet(Api, method, appKey, appSecret, accessToken, paramJson);

        System.out.println(object);
    }

    public void getCateProperty() {
        String Api = baseUrl + "/product/getCateProperty";
        String method = "product.getCateProperty";

        TreeMap<String, String> paramJson = new TreeMap<>();

        paramJson.put("first_cid", "20018");
        paramJson.put("second_cid", "20313");
        paramJson.put("third_cid", "22983");

        JSONObject object = DyApiClient.DYAoiGet(Api, method, appKey, appSecret, accessToken, paramJson);

        System.out.println(object);
    }

    private String decode(String cipherText, String orderId) {
        String Api = baseUrl + "/order/batchDecrypt";
        String method = "order.batchDecrypt";

        TreeMap<String, List<Object>> paramJson = new TreeMap<>();

        JSONObject obj = new JSONObject();
        obj.put("auth_id", orderId);
        obj.put("cipher_text", cipherText);
        List<Object> list = new ArrayList<>();
        list.add(obj);
        paramJson.put("cipher_infos", list);

        JSONObject object = DyApiClient.DYAoiGet(Api, method, appKey, appSecret, accessToken, paramJson);

        String decodeText = object.getJSONObject("data").getJSONArray("decrypt_infos").getJSONObject(0).getString("decrypt_text");

        System.out.println(decodeText);

        return decodeText;
    }

    private String getLogisticsId(String searchCode) {
        String Api = baseUrl + "/order/logisticsCompanyList";
        String method = "order.logisticsCompanyList";
        TreeMap<String, List<Object>> paramJson = new TreeMap<>();

        JSONObject object = DyApiClient.DYAoiGet(Api, method, appKey, appSecret, accessToken, paramJson);

        JSONArray list = object.getJSONArray("data");

        for (int i=0;i<list.size();i++){
            JSONObject item = list.getJSONObject(i);
            if (item.getString("code").equals(searchCode)) {
                return item.getString("id");
            }
        }

        System.out.println(list);

        return null;
    }

    public static void main(String[] args) throws Exception {
        TestDYDemo demo = new TestDYDemo();

//        demo.getOrderList();
//        demo.getCategory();
        demo.getCateProperty();
    }
}
